package cn.wildfire.chat.kit.conversation.forward;

import android.content.Intent;
import android.widget.Toast;

import androidx.lifecycle.ViewModelProviders;

import java.util.List;

import cn.wildfire.chat.app.extendsion.ui.GroupCreateSettingsDialog;
import cn.wildfire.chat.kit.common.OperateResult;
import cn.wildfire.chat.kit.contact.model.UIUserInfo;
import cn.wildfire.chat.kit.contact.pick.PickConversationTargetActivity;
import cn.wildfire.chat.kit.group.GroupViewModel;
import cn.wildfirechat.model.GroupInfo;

public class PickConversationTargetToForwardActivity extends PickConversationTargetActivity {
    // TODO 多选，单选
    // 先支持单选
    private boolean singleMode = true;

    /**
     * modify by HEKE , 2019/8/24 4:30
     * TODO 创建群信息窗口对象
     */
    private GroupCreateSettingsDialog groupCreateSettingsDialog;

    @Override
    protected void onContactPicked(List<UIUserInfo> initialCheckedUserInfos, List<UIUserInfo> newlyCheckedUserInfos) {
        if (singleMode && newlyCheckedUserInfos.size() > 1) {
            /**
             * modify by HEKE , 2019/8/24 3:21
             * TODO 设置群组信息窗口
             */
            GroupViewModel groupViewModel = ViewModelProviders.of(this).get(GroupViewModel.class);
            groupCreateSettingsDialog = new GroupCreateSettingsDialog(PickConversationTargetToForwardActivity.this, newlyCheckedUserInfos, groupViewModel,
                    GroupCreateSettingsDialog.CREATE_LIVEDATAS, this, new GroupCreateSettingsDialog.OnCreateGroupListener() {
                @Override
                public void result(final boolean created, final String target) {
                }

                @Override
                public void datasResult(OperateResult<String> result) {
                    if (result.isSuccess()) {
                        GroupInfo groupInfo = groupViewModel.getGroupInfo(result.getResult(), false);
                        Intent intent = new Intent();
                        intent.putExtra("groupInfo", groupInfo);
                        setResult(RESULT_OK, intent);
                        finish();
                    } else {
                        Toast.makeText(PickConversationTargetToForwardActivity.this, "create group error", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            groupCreateSettingsDialog.show();
        } else {
            Intent intent = new Intent();
            intent.putExtra("userInfo", newlyCheckedUserInfos.get(0).getUserInfo());
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        if (groupCreateSettingsDialog != null) {
            groupCreateSettingsDialog.dismiss();
            groupCreateSettingsDialog = null;
        }
        super.onDestroy();
    }

    // TODO 多选
    @Override
    public void onGroupPicked(List<GroupInfo> groupInfos) {
        Intent intent = new Intent();
        intent.putExtra("groupInfo", groupInfos.get(0));
        setResult(RESULT_OK, intent);
        finish();
    }
}
